package www.Zheng;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

/**
 * @Title: Demo8
 * @Author 郑雨杭
 * @Package www.Zheng
 * @Date 2024/7/8 9:48
 * @description:
 */
public class Demo8 {
    public static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        System.out.print("请输入需要操作文件夹的地址：");
        String path = scanner.nextLine();
        File file = new File(path);
        if(!file.exists()){
            System.out.println("操作失败，该地址不合法");
        } else if(file.isFile()){
            System.out.println("操作失败，该文件类型不合法");
        }else{
            String find = "test.txt";
           bianLi(file,find);
        }
    }

    public static void bianLi(File file,String find){
        if(file.isFile()){
            if(file.getName().equals(find)){
                try {
                    System.out.print("需要删除文件" + file.getCanonicalPath() + "么？y删除,n不删除:");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                String s = scanner.nextLine();
                if("y".equals(s)){
                    file.delete();
                    System.out.println("删除成功！！！");
                }else {
                    System.out.println("文件未被删除");
                }
            }
        }else{
            for (File listFile : file.listFiles()) {
                bianLi(listFile,find);
            }
        }
    }
}
